const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],
  /**
   * 页面的初始数据
   */
  data: {
    scene: "",
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("登陆页", options);
    this.data.scene = options.scene || ""

  },
  check_click() {
    console.log('check_click');
    this.setData({
      checked: !this.data.checked,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  login_click() {
    if (!this.data.checked) {
      return wx.showToast({
        title: '请先同意协议',
        icon: "none"

      })
    }
  },
  cancel_click() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  getPhoneNumber(e) {
    console.log("getPhoneNumber", e);
    let that = this;

    if (!e.detail.code) {
      return wx.showToast({
        title: '没有获取到手机号',
        icon: 'error'
      })
    }
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          console.log('wx.login', res);
          console.log({
            loginCode: res.code,
            phoneCode: e.detail.code
          });
          that.request({
            url: that.data.host + '/v1/authentication/login/wx',
            data: {
              loginCode: res.code,
              phoneCode: e.detail.code,
              inviterUserId: that.data.scene
            },
            method: 'post',

          }, res1 => {
            console.log('登陆接口', res1);
            wx.setStorage({
              key: 'Authorization',
              data: res1.data.accessToken
            })
            wx.setStorageSync('userInfo', res1.data)
            getApp().globalData.userInfo = {
              avatar: res1.data.avatar,
              id: res1.data.id,
              uid: res1.data.uid,
              phone: res1.data.phone,
              nickName: res1.data.nickName,
              userName: res1.data.userName,
            }
            wx.switchTab({
              url: '/pages/home/home',
            })
          })
        } else {

          wx.showToast({
            title: '授权失败',
            icon: 'error'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

})